using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace Fusters_Log_Splitter
{
    public partial class FusterSplitter : Form
    {

        public FusterSplitter()
        {
            InitializeComponent();
        }

        SettingsManager mySettingsMan = new SettingsManager();
        OpenFileDialog _fileLocation = new OpenFileDialog();
        FolderBrowserDialog _outputFileLocation = new FolderBrowserDialog();

        delegate void SetTextCallback(string text, bool append);

        private void button_browse_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "WoW log file (*.txt)|*.txt";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox_fileLocation.Text = ofd.FileName;

                if (outputFileLocation.Text == "Select the output path for split logfiles.")
                {
                    outputFileLocation.Text = System.IO.Path.GetDirectoryName(ofd.FileName);
                    outputFileConfirm.Enabled = false;
                    outputFileLocation.Enabled = true;
                    outputFileChoose.Enabled = true;
                    _outputFileLocation.SelectedPath = System.IO.Path.GetDirectoryName(ofd.FileName);
                }

                _fileLocation = ofd;

                mySettingsMan.setSetting("DefaultInFile", _fileLocation.FileName);
                mySettingsMan.setSetting("DefaultOutPath", _outputFileLocation.SelectedPath);
            }

        }

        private void button_chop_Click(object sender, EventArgs e)
        {
            if (File.Exists(_fileLocation.FileName))
            {
                Application.UseWaitCursor = true;
                this.splitProgressBar.Value = 0;
                this.splitProgressBar.Style = FustersProgressBar.FustersProgressBar.myProgressBarStyles.Blocks;
                this.SetStatusText("Processing Log...", false);

                WoWLog myLog = new WoWLog(_fileLocation.FileName.Replace(_fileLocation.SafeFileName, ""), _fileLocation.SafeFileName);
                myLog.setOutParams(_outputFileLocation.SelectedPath, (int)hourShiftSelector.Value, (int)splitGap.Value, deleteCheckBox.Checked);

                BackgroundWorker bgw = new BackgroundWorker();
                bgw.DoWork += new DoWorkEventHandler(myLog.LogSplitTemporalThreaded);
                bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
                bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
                bgw.WorkerReportsProgress = true;
                bgw.RunWorkerAsync();
            }
            else
            {
                Application.UseWaitCursor = false;
                this.splitProgressBar.Value = 100;
                this.splitProgressBar.Style = FustersProgressBar.FustersProgressBar.myProgressBarStyles.Gradient;
                this.splitProgressBar.GradientTop = Color.Red;
                this.splitProgressBar.GradientBottom = Color.Red;
                this.SetStatusText("Log not found!", false);
            }

        }

        void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.SetStatusText("Splitting: ", true);
            this.splitProgressBar.Increment(1);
        }

        void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.splitProgressBar.Value = 100;
            this.SetStatusText("Completed!!!", false);
            Application.UseWaitCursor = false;
        }

        private void outputFileChoose_Click(object sender, EventArgs e)
        {
            _outputFileLocation.SelectedPath = outputFileLocation.Text;
            _outputFileLocation.ShowDialog();
            outputFileLocation.Text = _outputFileLocation.SelectedPath;
            mySettingsMan.setSetting("DefaultOutPath", _outputFileLocation.SelectedPath);
            outputFileConfirm.Enabled = false;
        }

        private void outputFileConfirm_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(outputFileLocation.Text))
                _outputFileLocation.SelectedPath = outputFileLocation.Text;
            else
            {
                _outputFileLocation.ShowDialog();
                outputFileLocation.Text = _outputFileLocation.SelectedPath;
            }

            mySettingsMan.setSetting("DefaultOutPath", _outputFileLocation.SelectedPath);
            outputFileConfirm.Enabled = false;
        }

        private void outputFileLocation_TextChanged(object sender, EventArgs e)
        {
            outputFileConfirm.Enabled = true;
        }

        private void FusterSplitter_Load(object sender, EventArgs e)
        {
            _fileLocation.FileName = mySettingsMan.getSetting("DefaultInFile");
            _outputFileLocation.SelectedPath = mySettingsMan.getSetting("DefaultOutPath");
            splitGap.Value = Convert.ToInt32(mySettingsMan.getSetting("DefaultSplitGap"));
            hourShiftSelector.Value = Convert.ToInt32(mySettingsMan.getSetting("DefaultHourShift"));

            if (Convert.ToInt32(mySettingsMan.getSetting("DeleteLog")) == 1)
            {
                deleteCheckBox.Checked = true;
            }

            textBox_fileLocation.Text = _fileLocation.FileName;
            outputFileLocation.Text = _outputFileLocation.SelectedPath;
            outputFileLocation.Enabled = true;
            outputFileChoose.Enabled = true;
            outputFileConfirm.Enabled = false;

            this.SetStatusText("Ready to Split!", false);
            this.splitProgressBar.Value = 0;
        }

        private void deleteCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (deleteCheckBox.Checked == true)
            {
                mySettingsMan.setSetting("DeleteLog", "1");
            } 
            else
            {
                mySettingsMan.setSetting("DeleteLog", "0");
            }
        }

        private void SetStatusText(string text, bool append)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.splitProgressBar.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetStatusText);
                this.splitProgressBar.Invoke(d, new object[] { text , append});
            }
            else
            {
                this.splitProgressBar.setCustomText(text, append);
            }
        }

        private void hourShiftSelector_ValueChanged(object sender, EventArgs e)
        {
            mySettingsMan.setSetting("DefaultHourShift", hourShiftSelector.Value.ToString());
        }

        private void splitGap_ValueChanged(object sender, EventArgs e)
        {
            mySettingsMan.setSetting("DefaultSplitGap", splitGap.Value.ToString());
        }


    }
}